package net.minecraft.src;

import java.util.*;

public class BlockTransmiter extends  BlockContainer
{

	private boolean  powerState;

	private static String stampKey[]= null;
	private String  key = new String("");
    public int getRenderBlockPass()
    {
        return 0;
    }
    protected BlockTransmiter(int i, int j, boolean power)
    {
        super(i, j,Material.circuits);
        powerState = power;
    }
    
    private boolean func_30002_h(World world, int i, int j, int k)
    {
        if ( world.isBlockIndirectlyProvidingPowerTo(i, j + 1, k, 0))
        {
            return true;
        }
        if ( world.isBlockIndirectlyProvidingPowerTo(i, j - 1, k, 0))
        {
            return true;
        }
        if (world.isBlockIndirectlyProvidingPowerTo(i, j, k - 1, 2))
        {
            return true;
        }
        if (world.isBlockIndirectlyProvidingPowerTo(i, j, k + 1, 3))
        {
            return true;
        }
        if (world.isBlockIndirectlyProvidingPowerTo(i - 1, j, k, 4))
        {
            return true;
        }
        return world.isBlockIndirectlyProvidingPowerTo(i + 1, j, k, 5);
    }
    
    public void onNeighborBlockChange(World world, int i, int j, int k, int l)
    {
        super.onNeighborBlockChange(world, i, j, k, l);
        world.scheduleBlockUpdate(i, j, k, blockID, tickRate());
    }
    
    public int tickRate()
    {
        return 1;
    }
    
    
    public TileEntity getBlockEntity()
    {
        try
        {
        	 return (TileEntity) new TileTransmiterStamp();
           
        }
        catch (Exception exception)
        {
            throw new RuntimeException(exception);
        }
    }
    
    public void onBlockAdded(World world, int i, int j, int k)
    {
    	
        if (world.getBlockMetadata(i, j, k) == 0)
        {
            super.onBlockAdded(world, i, j, k);
            if(stampKey != null)
            {
            	
            	TileTransmiterStamp tileReceiverTransmiter = (TileTransmiterStamp)world.getBlockTileEntity(i, j, k);
            	tileReceiverTransmiter.setTileString(stampKey);

            	stampKey = null;
            }
        }
    }
    
    public boolean blockActivated(World world, int i, int j, int k, EntityPlayer entityplayer)
    {

        return true;
    }
    public int idDropped(int i, Random random, int j)
    {
        return mod_AsPj.blockTransmiterIdle.blockID;
    }

    public void updateTick(World world, int i, int j, int k, Random random)
    {
        boolean flag = func_30002_h(world, i, j, k);
        if (powerState != flag)
        {

        		TileTransmiterStamp	tileReceiverTransmiter = (TileTransmiterStamp)world.getBlockTileEntity(i, j, k);
        		key = tileReceiverTransmiter.getTileString();

            	ArrayList<Coords> coordsArray= BlockReceiver.codeToCoords.get(key);
            	
            	if(coordsArray != null)
            	{
	            	ListIterator<Coords> iterator =
	            			coordsArray.listIterator();
	   
	        	    while(iterator.hasNext())
	        	    {
	        	    	Coords  upDate = iterator.next();
	        	    	BlockReceiver.receiversToUpdate.add(upDate);
	        	    	world.scheduleBlockUpdate(upDate.x, upDate.y, upDate.z, world.getBlockId(upDate.x, upDate.y, upDate.z),1);
	
	        	    }
            	}
        	    
    			int newBlockId =
    					this.blockID == mod_AsPj.blockTransmiterIdle.blockID ? mod_AsPj.blockTransmiterPowered.blockID : mod_AsPj.blockTransmiterIdle.blockID;
    			stampKey = tileReceiverTransmiter.getTileStringArray();
    			world.setBlockAndMetadataWithNotify(i, j, k, newBlockId , world.getBlockMetadata(i, j, k));
    			
        } 		
       
    }
    

    
    public boolean renderAsNormalBlock()
    {
        return false;
    }
   

    public boolean isOpaqueCube()
    {
        return false;
    }

    
}